package transformer

import (
	"alertmanager-webhook/model"
	"bytes"
	"fmt"
)

func TransformToMarkdown(notification model.Notification) (markdown *model.DingTalkMarkdown, err error) {
	groupKey := notification.GroupKey
	status := notification.Status
	annotations := notification.CommonAnnotations

	var buffer bytes.Buffer
	buffer.WriteString(fmt.Sprintf("### 通知组%s(当前状态：%s)\n", groupKey, status))
	buffer.WriteString(fmt.Sprintf("### 告警项：\n"))

	for _, alert := range notification.Alerts {
		annotations = alert.Annotations
		buffer.WriteString(fmt.Sprintf("##### %s\n > %s\n", annotations["summary"], annotations["description"]))
	}
	markdown = &model.DingTalkMarkdown{
		MsgType: "markdown",
		Markdown: &model.Markdown{
			Title: fmt.Sprintf("通知组%s(当前状态：%s)", groupKey, status),
			Text:  buffer.String(),
		},
		At: &model.At{
			IsAtAll: false,
		},
	}
	return
}
